﻿using MockPrjTeam1.Biz;
using MockPrjTeam1.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MockPrjTeam1.Pages.Views
{
    public partial class RegionDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var biz = new CountryBiz();
                var results = biz.GetName();
                listCountry.DataSource = results;
                listCountry.DataBind();

                var regionId = Convert.ToString(Request.QueryString["id"]);
                if (!string.IsNullOrEmpty(regionId))
                {
                    var biz1 = new RegionBiz();
                    var reg = new Region();
                    reg = biz1.GetOne(Convert.ToInt32(regionId));
                    listCountry.SelectedValue = reg.Country;
                    txtRegionName.Text = reg.Name;
                    txtDescription.Text = reg.Description;
                    if (reg.Active)
                        cbActive.Checked = true;
                    else
                        cbActive.Checked = false;

                    var biz2 = new DistrictBiz();
                    var results2 = biz2.GetActive(Convert.ToInt32(regionId));
                    grvDistrict.DataSource = results2;
                    grvDistrict.DataBind();

                    var save = Convert.ToString(Request.QueryString["save"]);
                    if (!string.IsNullOrEmpty(save))
                        ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
                }
                else
                    ClientScript.RegisterStartupScript(GetType(), "id1", "districtTab.style.display='none';", true);
            }
        }      

        protected void Save(object sender, EventArgs e)
        {
            var biz = new RegionBiz();

            var id1 = Convert.ToString(Request.QueryString["id"]);
            if (!string.IsNullOrEmpty(txtRegionName.Text))
            {
                if (!string.IsNullOrEmpty(id1))
                {
                    if (biz.CheckName(txtRegionName.Text, Convert.ToInt32(id1)))
                    {
                        var entity = new Region();
                        entity.ID = Convert.ToInt32(id1);
                        entity.Name = txtRegionName.Text;
                        entity.Description = txtDescription.Text;
                        entity.Country = listCountry.SelectedValue;
                        if (cbActive.Checked)
                            entity.Active = true;
                        else
                            entity.Active = false;
                        if (biz.Edit(entity))
                            Response.Redirect("../Views/RegionList.aspx");
                    }
                    else
                    {
                        lbMessage.Text = "Exists Name";
                        ClientScript.RegisterStartupScript(GetType(), "id1", "districtTab.style.display='none';", true);
                    }

                }

                else
                {
                    if (biz.CheckName(txtRegionName.Text, 0))
                    {
                        var entity = new Region();
                        entity.Name = txtRegionName.Text;
                        entity.Description = txtDescription.Text;
                        entity.Country = listCountry.SelectedValue;
                        if (cbActive.Checked)
                            entity.Active = true;
                        else
                            entity.Active = false;
                        if (biz.Create(entity))
                            Response.Redirect("../Views/RegionList.aspx");
                    }
                    else
                    {
                        lbMessage.Text = "Exists Name";
                        ClientScript.RegisterStartupScript(GetType(), "id1", "districtTab.style.display='none';", true);
                    }
                }
            }
            else
            {
                lbMessage.Text = "Name is not null!";
                ClientScript.RegisterStartupScript(GetType(), "id1", "districtTab.style.display='none';", true);
            }
        }

        protected void GetAllRegionDistrict(object sender, EventArgs e)
        {
            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
            {
                var biz = new DistrictBiz();
                var results = biz.GetAllRegionDistrict(Convert.ToInt32(regionId));
                grvDistrict.DataSource = results;
                grvDistrict.DataBind();
            }
            else
            {
                var biz = new DistrictBiz();
                var results = biz.GetActive(Convert.ToInt32(regionId));
                grvDistrict.DataSource = results;
                grvDistrict.DataBind();
            }

            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void PageChanging(object sender, GridViewPageEventArgs e)
        {
            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
            {
                var biz = new DistrictBiz();
                var results = biz.GetAllRegionDistrict(Convert.ToInt32(regionId));
                grvDistrict.PageIndex = e.NewPageIndex;
                grvDistrict.DataSource = results;
                grvDistrict.DataBind();
            }
            else
            {
                var biz = new DistrictBiz();
                var results = biz.GetActive(Convert.ToInt32(regionId));
                grvDistrict.PageIndex = e.NewPageIndex;
                grvDistrict.DataSource = results;
                grvDistrict.DataBind();
            }
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void BtnCreateClick(object sender, EventArgs e)
        {
            string regionId = Convert.ToString(Request.QueryString["id"]);
            if (!string.IsNullOrEmpty(regionId))
                Response.Redirect("../Forms/DistrictDetail.aspx?rid=" + regionId);
            else
                Response.Redirect("../Views/RegionList.aspx");
        }

        protected void Filter09(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 2, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 2, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void FilterAE(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 3, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 3, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void FilterFJ(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 4, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 4, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void FilterKN(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 5, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 5, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void FilterOR(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 6, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 6, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void FilterSV(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 7, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 7, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }

        protected void FilterWZ(object sender, EventArgs e)
        {
            var biz = new DistrictBiz();
            List<District> results = null;

            var regionId = Convert.ToString(Request.QueryString["id"]);

            if (cbInActive.Checked)
                results = biz.Filter(Convert.ToInt32(regionId), 8, 0);
            else
                results = biz.Filter(Convert.ToInt32(regionId), 8, 1);

            grvDistrict.DataSource = results;
            grvDistrict.DataBind();
            ClientScript.RegisterStartupScript(GetType(), "id", "openDistrict(true);", true);
        }
    }
}